<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 各种配置通用管理
 *
 * @icon fa fa-circle-o
 */
class Configure extends Backend {

    /**
     * Configure模型对象
     * @var \app\admin\model\Configure
     */
    protected $model = null;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\Configure;
    }

    public function buhuo() {
        if ($this->request->isPost()) {
            $data['zongjian'] = input('zongjian', 0);
            $data['jine'] = input('jine', 0);
            $data['zhekou'] = input('zhekou', 0);

            db('configure')->where('key', 'buhuo')->update(['desc' => json_encode($data)]);
            $this->success();
        } else {
            $info = db('configure')->where('key', 'buhuo')->find();
            $info = json_decode($info['desc'], true);
            $this->assign('info', $info);
            return $this->view->fetch();
        }
    }

    public function huosun() {
        if ($this->request->isPost()) {

            $infos = input('infos/a');

            foreach ($infos as $key => $val) {
                db('damage_level')->where('damage_level_id', $key)->update(['amount' => $val['amount']]);
            }

            $jine = input('jine', 0);

            db('configure')->where('key', 'huosun')->update(['desc' => $jine]);
            $this->success();
        } else {

            $list = db('damage_level')->order('amount desc')->select();

            $info = db('configure')->where('key', 'huosun')->value('desc');

            $this->assign('list', $list);
            $this->assign('info', $info);
            return $this->view->fetch();
        }
    }

    public function jifen() {
        if ($this->request->isPost()) {
            $data['zengsong'] = input('zengsong', 0);
            $data['dikou'] = input('dikou', 0);
            db('configure')->where('key', 'jifen')->update(['desc' => json_encode($data)]);
            $this->success();
        } else {
            $info = db('configure')->where('key', 'jifen')->find();
            $info = json_decode($info['desc'], true);
            $this->assign('info', $info);
            return $this->view->fetch();
        }
    }

    public function fenxiang() {
        if ($this->request->isPost()) {
            $data['title'] = input('title');
            $data['sub_title'] = input('sub_title');
            $data['share_logo'] = input('share_logo');
            db('configure')->where('key', 'fenxiang')->update(['desc' => json_encode($data)]);
            $this->success();
        } else {
            $info = db('configure')->where('key', 'fenxiang')->find();
            $info = json_decode($info['desc'], true);
            $this->assign('row', $info);
            return $this->view->fetch();
        }
    }

    public function xinren() {
        if ($this->request->isPost()) {
            $kaiguan['desc'] = input('xinrenyouhuiquankaiguan', 0);

            $data['desc'] = input('xinrenyouhuiquan', 0);

            db('configure')->where('key', 'xinrenyouhuiquan')->update($data);
            db('configure')->where('key', 'xinrenyouhuiquankaiguan')->update($kaiguan);
            $this->success();
        } else {
            $kaiguan = db('configure')->where('key', 'xinrenyouhuiquankaiguan')->value('desc');
            $info = db('configure')->where('key', 'xinrenyouhuiquan')->find();
            $this->assign('info', $info);
            $this->assign('kaiguan', $kaiguan);
            return $this->view->fetch();
        }
    }

    public function shoujihao() {
        if ($this->request->isPost()) {
            $kaiguan['desc'] = input('bangdingshoujihao', 0);

            $data['desc'] = input('bangdingshoujihaoyouhuiquan', 0);

            db('configure')->where('key', 'bangdingshoujihao')->update($data);
            db('configure')->where('key', 'bangdingshoujihaoyouhuiquan')->update($kaiguan);
            $this->success();
        } else {
            $kaiguan = db('configure')->where('key', 'bangdingshoujihao')->value('desc');
            $info = db('configure')->where('key', 'bangdingshoujihaoyouhuiquan')->find();
            $url = $this->request->domain() . url('index/pub/bind_phone');

            $this->assign('url', $url);
            $this->assign('info', $info);
            $this->assign('kaiguan', $kaiguan);
            return $this->view->fetch();
        }
    }

    public function shiming() {
        if ($this->request->isPost()) {
            $kaiguan['desc'] = input('shimingrenzhen', 0);

            $data['desc'] = input('shimingrenzhenyouhuiquan', 0);

            db('configure')->where('key', 'shimingrenzhen')->update($data);
            db('configure')->where('key', 'shimingrenzhenyouhuiquan')->update($kaiguan);
            $this->success();
        } else {
            $kaiguan = db('configure')->where('key', 'shimingrenzhen')->value('desc');
            $info = db('configure')->where('key', 'shimingrenzhenyouhuiquan')->find();
            $url = $this->request->domain() . url('index/pub/user_auth');
            $this->assign('url', $url);
            $this->assign('info', $info);
            $this->assign('kaiguan', $kaiguan);
            return $this->view->fetch();
        }
    }

}
